package com.zhdl.modules.system.web.controller;

import cn.hutool.json.JSONUtil;
import com.zhdl.common.util.WebResult;
import com.zhdl.modules.common.logging.service.OptRecordService;
import com.zhdl.modules.system.entity.CustomerUser;
import com.zhdl.modules.system.service.CustomerUserService;
import com.zhdl.modules.system.web.request.UserLoginRequest;
import com.zhdl.modules.system.web.request.UserSelectRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping(path = "/customerUser", produces = MediaType.APPLICATION_JSON_VALUE)
public class CustomerUserController {

    @Resource
    private CustomerUserService customerUserService;

    @Resource
    private OptRecordService optRecordService;

    /**
     * 测试接口
     *
     * @return 测试服务是否启动
     */
    @GetMapping("/demo")
    public WebResult<String> InterfaceDebugging() {
        return WebResult.okResult("当前接口通讯正常");
    }

    /**
     * 用户登陆
     *
     * @param login 登陆信息
     * @return 用户信息/登陆提示
     */
    @PostMapping("/login")
    public WebResult<String> userLogin(@RequestBody UserLoginRequest login) {
        //判断是否为游客状态
        if (!login.isTourist()) {
            CustomerUser customerUserByUserName = customerUserService.findCustomerUserByUserName(login.getUsername());
            if (customerUserByUserName != null) {
                if (customerUserByUserName.getPassword().equals(login.getPassword())) {
                    String jsonStr = JSONUtil.toJsonStr(customerUserByUserName);
                    return WebResult.okResult(jsonStr);
                } else {
                    return WebResult.okResult("error");
                }
            } else {
                return WebResult.okResult("error");
            }
        } else {
            CustomerUser customerUser = new CustomerUser();
            customerUser.setId("999");
            customerUser.setUsername("666666");
            customerUser.setPassword("666666");
            customerUser.setRealName("游客");
            customerUser.setIdentity(2);
            String jsonStr = JSONUtil.toJsonStr(customerUser);
            return WebResult.okResult(jsonStr);
        }
    }

    /**
     * 创建用户
     *
     * @param customerUser 用户创建
     * @return 用户信息
     */
    @PostMapping("/insertCustomerUser")
    public WebResult<CustomerUser> insertCustomerUser(@RequestBody CustomerUser customerUser) {
        CustomerUser insert = customerUserService.insert(customerUser);
        return WebResult.okResult(insert);
    }

    /**
     * 更新用户信息
     *
     * @param customerUser 用户更新
     * @return 更新后的用户信息
     */
    @PostMapping("/updateCustomerUser")
    public WebResult<CustomerUser> updateCustomerUser(@RequestBody CustomerUser customerUser) {
        CustomerUser update = customerUserService.update(customerUser, customerUser.getId());
        return WebResult.okResult(update);
    }

    /**
     * 删除用户信息
     *
     * @param userId 用户Id
     * @return 用户Id
     */
    @PostMapping("/deleteCustomerUser")
    public WebResult<String> deleteCustomerUser(@RequestParam String userId) {
        customerUserService.deleteById(userId);
        return WebResult.okResult("删除当前用户信息" + userId);
    }

    /**
     * 查询用户列表
     *
     * @return List<>用户信息</>
     */
    @GetMapping("/findAllList")
    public WebResult<List<CustomerUser>> findAllList() {
        List<CustomerUser> customerUsers = customerUserService.selectAll();
        return WebResult.okResult(customerUsers);
    }


    /**
     * 分页查询用户列表
     *
     * @param request 查询条件
     * @return /
     */
    @PostMapping("/user/findAllPage")
    public WebResult<Page<CustomerUser>> findAllByPage(@RequestBody UserSelectRequest request) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createData");
        Pageable pageable = PageRequest.of(request.getPageNumber(), request.getPageSize(), sort);
        Page<CustomerUser> allPage = customerUserService.findAllPage(request.getQueryCriteria(), pageable);
        return WebResult.okResult(allPage);
    }


}
